<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
include "header.php";
include "/library/product.php";

if(isset($_GET['delete'])){
    delete($_GET['id']);
   // header('location: product.php');
}
$page = 0;
$perpage=10;
$sort="";
$order ="";

if(isset($_GET['search'])){
                $order= $order . " WHERE name LIKE '%".$_GET['search']."%' OR product_id LIKE '%".product_id_check_out($_GET['search'])."%' ";
}
if(isset($_GET['sort'])){
                if($_GET['sort']==0) $sort = "product_id";
                else if($_GET['sort']==1) $sort = "name";
                $order =$order . "ORDER BY ".$sort;
}else
    $order=$order . "ORDER BY product_id";
$temp = showAll($order);
$allContent =mysql_num_rows($temp);
if(isset($_GET['page'])){
                $page =  $_GET['page'] ;

                $start = $page * $perpage;
                
                $order = $order . " LIMIT ".$start.", ".$perpage;

}else
                $order = $order . " LIMIT 0, ".$perpage;
?>

<script type="text/javascript">
    $(document).ready(function(){
        $('#sortSelect123').change(function(){
            $('#receiptForm').submit();
        });
    });



</script>
<div id="receiptOrder" >
    <form action="product.php" method="get" id="receiptForm">
    เรียงตาม

    <select name="sort" id="sortSelect123">

        <option value="0" <?php if($sort=='product_id') echo "selected=selected";?>>รหัสสินค้า</option>

        <option value="1" <?php if($sort=='name') echo "selected=selected";?>>ชื่อสินค้า</option>

    </select>
    <?php if(isset($_GET['search'])) { ?> <input type="hidden" name="search" value="<?=$_GET['search']?>"> <?php } ?>
    <?php if(isset($_GET['page'])) { ?> <input type="hidden" name="page" value="<?=$_GET['page']?>"> <?php } ?>
    </form>
</div>

<div id="searchDiv">
    <form action="product.php" method="get">
        <input type="text" name="search" size="80 value="<?php if(isset($_GET['search'])) { echo $_GET['search'];  } ?>">
        <input type="submit" value="ค้นหา">
        <?php if(isset($_GET['sort'])) { ?> <input type="hidden" name="sort" value="<?=$_GET['sort']?>"> <?php } ?>
    </form>
</div>
<div id="pageNumber">
    หน้า 
    <?php
    
    $allPage = ceil($allContent/$perpage) ;

    for($i = 1 ; $i <= $allPage ;$i++){
        $te=$i-1;
        $opt ="";
        if(isset($_GET['sort']))
            $opt = "&sort=".$_GET['sort'];
        if(isset($_GET['search']))
            $opt = $opt . "&search=".$_GET['search'];
        if($page==$te)
            echo $i." ";
        else
            echo "<a href='product.php?page=$te$opt'>$i </a>";
    }
    ?>
</div>
<div name="multivalue" align="center" width="100%" >
    <table   id="tableShow">
        <tr>
            <th id="longBlock">รหัสสินค้า </th>
            <th id="detail">ชื่อสินค้า</th>
            <th>จำนวน</th>
            <th >ประเภทการขาย</th>
            <th >หน่วยการขาย</th>           
            <th >อัตราต่อหนวย</th>
            <th >ราคาทุน</th>
            <th >ราคาขาย</th>
            <th></th>
           <!-- <th></th> -->
        </tr>
        <?php
            
            $product = showAll($order);
            while ($print = mysql_fetch_array($product)){
                $print['product_id']=  product_id_check($print['product_id']);
                
                ?>

        <tr>
            <td><?=$print['product_id']; ?>
            <td ><?=$print['name']; ?></td>
            <td ><?=$print['amount']; ?></td>
            
                <?php
                    $product_type = showAllType($print['id']);
                    $ck=0;

                    while ($printType = mysql_fetch_array($product_type)){
                        if($ck!=0){
                ?>
                        <tr>
                            <td ></td>
                            <td ></td>
                            <td ></td>
                    <?php  } $ck++;?>
                    <td id="product_typeView"><?=$printType['name']; ?></td>
                    <td id="numberBlock"><?=$printType['nameunit']; ?></td>
                    <td id="numberBlock"><?=$printType['ratio']; ?></td>
                    <td id="numberBlock"><?=$printType['asset']; ?></td>                    
                    <td id="numberBlock"><?=$printType['buyprice']; ?></td>
               <?php  if($ck==1){ ?>

                        <td id="numberBlock"><a href="product_add.php?editform=&id=<?=$print['id'];?>"><input type="button" value="แก้ไข"></a></td>
               <!--     <td><a href="product.php?delete=&id=<?=$print['id'];?>">ลบ</a></td>  -->
               <?php  }else {  ?>
                    <td id="numberBlock"></td>
               <!--      <td></td> -->
                    <?php } ?>
                </tr>
              
                <?php } ?>
                <tr >

                   <td  id="closeList"></td>
                   <td  id="closeList"></td>
                   <td  id="closeList"></td>
                   <td  id="closeList"></td>
                   <td  id="closeList" ></td>
                   <td  id="closeList"></td>
                   <td  id="closeList"></td>
                   <td  id="closeList"></td>
                   <td  id="closeList"></td>


               </tr>
        <?php }  ?>
    </table>
   
    <p>
    </p>
  
</div>
